package com.cskaoyan.config;

import com.cskaoyan.converter.String2DateConverter;
import com.cskaoyan.interceptor.CustomHandlerInterceptor1;
import com.cskaoyan.interceptor.CustomHandlerInterceptor2;
import org.hibernate.validator.HibernateValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.format.FormatterRegistry;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.PostConstruct;

/**
 * 通常实现一个接口WebMvcConfigurer
 *         要使用该接口提供的方法完成一些配置，比如mvc:resources\mvc:interceptors
 * @author stone
 * @date 2022/03/28 10:05
 */
//@Configuration
@ComponentScan("com.cskaoyan.controller")
@EnableWebMvc
public class MvcConfiguration implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //                         mapping属性值                                 location属性值
        registry.addResourceHandler("/pic/**").addResourceLocations("file:d:/spring/");
        registry.addResourceHandler("/pic2/**").addResourceLocations("classpath:/");
    }

    /*//1.取出来
    @Autowired
    ConfigurableConversionService conversionService;
    //2.增加上
    @PostConstruct
    public void addConverters() {
        conversionService.addConverter(new String2DateConverter());
    }
    //3.放回去
    @Bean
    @Primary //首要的
    public ConfigurableConversionService conversionService() {
        return conversionService;
    }*/
    //也是由WebMvcConfigurer提供的方法
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new String2DateConverter());
    }

    //HandlerInterceptor的配置
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //默认作用范围是全局
        registry.addInterceptor(new CustomHandlerInterceptor1());
        //addPathPatterns可以指定作用范围
        //顺序仍然是书写顺序
        registry.addInterceptor(new CustomHandlerInterceptor2()).addPathPatterns("/user/**");
    }

    @Override
    public Validator getValidator() {
        //虚假的FactoryBean
        LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
        localValidatorFactoryBean.setProviderClass(HibernateValidator.class);
        return localValidatorFactoryBean;
    }

    @Bean
    public CommonsMultipartResolver multipartResolver() {
        return new CommonsMultipartResolver();
    }
}
